/* Include files */

#include "tanks_sfun.h"
#include "c3_tanks.h"
#define CHARTINSTANCE_CHARTNUMBER      (chartInstance.chartNumber)
#define CHARTINSTANCE_INSTANCENUMBER   (chartInstance.instanceNumber)
#include "tanks_sfun_debug_macros.h"

/* Type Definitions */

/* Named Constants */
#define c3_IN_NO_ACTIVE_CHILD          (0)

/* Variable Declarations */

/* Variable Definitions */
static SFc3_tanksInstanceStruct chartInstance;

/* Function Declarations */
static void initialize_c3_tanks(void);
static void initialize_params_c3_tanks(void);
static void enable_c3_tanks(void);
static void disable_c3_tanks(void);
static void update_debugger_state_c3_tanks(void);
static const mxArray *get_sim_state_c3_tanks(void);
static void set_sim_state_c3_tanks(const mxArray *c3_st);
static void finalize_c3_tanks(void);
static void sf_c3_tanks(void);
static void init_script_number_translation(uint32_T c3_machineNumber, uint32_T
  c3_chartNumber);
static const mxArray *c3_sf_marshall(void *c3_chartInstance, void *c3_u);
static const mxArray *c3_b_sf_marshall(void *c3_chartInstance, void *c3_u);
static const mxArray *c3_c_sf_marshall(void *c3_chartInstance, void *c3_u);
static void init_dsm_address_info(void);

/* Function Definitions */
static void initialize_c3_tanks(void)
{
  _sfTime_ = (real_T)ssGetT(chartInstance.S);
  chartInstance.c3_tempoant_not_empty = false;
  chartInstance.c3_yant_not_empty = false;
  chartInstance.c3_is_active_c3_tanks = 0U;
}

static void initialize_params_c3_tanks(void)
{
}

static void enable_c3_tanks(void)
{
  _sfTime_ = (real_T)ssGetT(chartInstance.S);
}

static void disable_c3_tanks(void)
{
  _sfTime_ = (real_T)ssGetT(chartInstance.S);
}

static void update_debugger_state_c3_tanks(void)
{
}

static const mxArray *get_sim_state_c3_tanks(void)
{
  return NULL;
}

static void set_sim_state_c3_tanks(const mxArray *c3_st)
{
  sf_mex_destroy(&c3_st);
}

static void finalize_c3_tanks(void)
{
}

static void sf_c3_tanks(void)
{
  uint8_T c3_previousEvent;
  real_T c3_tempo;
  real_T c3_nargout = 1.0;
  real_T c3_nargin = 1.0;
  real_T c3_y;
  const mxArray *c3_recv_opc;
  real_T c3_d0;
  real_T *c3_b_y;
  real_T *c3_b_tempo;
  c3_b_y = (real_T *)ssGetOutputPortSignal(chartInstance.S, 1);
  c3_b_tempo = (real_T *)ssGetInputPortSignal(chartInstance.S, 0);
  _sfTime_ = (real_T)ssGetT(chartInstance.S);
  _SFD_CC_CALL(CHART_ENTER_SFUNCTION_TAG,1);
  _SFD_DATA_RANGE_CHECK(*c3_b_y, 0U);
  _SFD_DATA_RANGE_CHECK(*c3_b_tempo, 1U);
  c3_previousEvent = _sfEvent_;
  _sfEvent_ = CALL_EVENT;
  _SFD_CC_CALL(CHART_ENTER_DURING_FUNCTION_TAG,1);
  c3_tempo = *c3_b_tempo;
  sf_debug_symbol_scope_push(6U, 0U);
  sf_debug_symbol_scope_add("yant", &chartInstance.c3_yant, c3_c_sf_marshall);
  sf_debug_symbol_scope_add("tempoant", &chartInstance.c3_tempoant,
    c3_b_sf_marshall);
  sf_debug_symbol_scope_add("nargout", &c3_nargout, c3_sf_marshall);
  sf_debug_symbol_scope_add("nargin", &c3_nargin, c3_sf_marshall);
  sf_debug_symbol_scope_add("y", &c3_y, c3_sf_marshall);
  sf_debug_symbol_scope_add("tempo", &c3_tempo, c3_sf_marshall);
  CV_EML_FCN(0, 0);

  /*  This block supports an embeddable subset of the MATLAB language. */
  /*  See the help menu for details.  */
  _SFD_EML_CALL(0,5);
  _SFD_EML_CALL(0,8);
  if (CV_EML_IF(0, 0, !chartInstance.c3_tempoant_not_empty)) {
    _SFD_EML_CALL(0,9);
    chartInstance.c3_tempoant = c3_tempo;
    chartInstance.c3_tempoant_not_empty = true;
  }

  _SFD_EML_CALL(0,12);
  if (CV_EML_IF(0, 1, !chartInstance.c3_yant_not_empty)) {
    _SFD_EML_CALL(0,13);
    chartInstance.c3_yant = 0.0;
    chartInstance.c3_yant_not_empty = true;
  }

  _SFD_EML_CALL(0,16);
  if (CV_EML_IF(0, 2, c3_tempo > chartInstance.c3_tempoant + 0.1)) {
    _SFD_EML_CALL(0,17);
    c3_y = 0.0;
    _SFD_EML_CALL(0,18);
    c3_recv_opc = sf_mex_call("recv_opc", 1U, 0U);
    sf_mex_import("recv_opc", sf_mex_dup(c3_recv_opc), &c3_d0, 1, 0, 0U, 0, 0U,
                  0);
    c3_y = c3_d0;
    sf_mex_destroy(&c3_recv_opc);
    _SFD_EML_CALL(0,19);
    chartInstance.c3_tempoant = c3_tempo;
    _SFD_EML_CALL(0,20);
    chartInstance.c3_yant = c3_y;
    _SFD_EML_CALL(0,21);
  } else {
    _SFD_EML_CALL(0,23);
    c3_y = chartInstance.c3_yant;
  }

  _SFD_EML_CALL(0,-23);
  sf_debug_symbol_scope_pop();
  *c3_b_y = c3_y;
  _SFD_CC_CALL(EXIT_OUT_OF_FUNCTION_TAG,1);
  _sfEvent_ = c3_previousEvent;
  sf_debug_check_for_state_inconsistency(_tanksMachineNumber_,
    chartInstance.chartNumber, chartInstance.instanceNumber);
}

static void init_script_number_translation(uint32_T c3_machineNumber, uint32_T
  c3_chartNumber)
{
}

static const mxArray *c3_sf_marshall(void *c3_chartInstance, void *c3_u)
{
  const mxArray *c3_y = NULL;
  real_T c3_b_u;
  const mxArray *c3_b_y = NULL;
  c3_y = NULL;
  c3_b_u = *((real_T *)c3_u);
  c3_b_y = NULL;
  sf_mex_assign(&c3_b_y, sf_mex_create("y", &c3_b_u, 0, 0U, 0U, 0U, 0));
  sf_mex_assign(&c3_y, c3_b_y);
  return c3_y;
}

static const mxArray *c3_b_sf_marshall(void *c3_chartInstance, void *c3_u)
{
  const mxArray *c3_y = NULL;
  real_T c3_b_u;
  const mxArray *c3_b_y = NULL;
  c3_y = NULL;
  c3_b_u = *((real_T *)c3_u);
  c3_b_y = NULL;
  if (!chartInstance.c3_tempoant_not_empty) {
    sf_mex_assign(&c3_b_y, sf_mex_create("y", NULL, 0, 0U, 1U, 0U, 2, 0, 0));
  } else {
    sf_mex_assign(&c3_b_y, sf_mex_create("y", &c3_b_u, 0, 0U, 0U, 0U, 0));
  }

  sf_mex_assign(&c3_y, c3_b_y);
  return c3_y;
}

static const mxArray *c3_c_sf_marshall(void *c3_chartInstance, void *c3_u)
{
  const mxArray *c3_y = NULL;
  real_T c3_b_u;
  const mxArray *c3_b_y = NULL;
  c3_y = NULL;
  c3_b_u = *((real_T *)c3_u);
  c3_b_y = NULL;
  if (!chartInstance.c3_yant_not_empty) {
    sf_mex_assign(&c3_b_y, sf_mex_create("y", NULL, 0, 0U, 1U, 0U, 2, 0, 0));
  } else {
    sf_mex_assign(&c3_b_y, sf_mex_create("y", &c3_b_u, 0, 0U, 0U, 0U, 0));
  }

  sf_mex_assign(&c3_y, c3_b_y);
  return c3_y;
}

const mxArray *sf_c3_tanks_get_eml_resolved_functions_info(void)
{
  const mxArray *c3_nameCaptureInfo = NULL;
  c3_ResolvedFunctionInfo c3_info[2];
  c3_ResolvedFunctionInfo (*c3_b_info)[2];
  const mxArray *c3_m0 = NULL;
  int32_T c3_i0;
  c3_ResolvedFunctionInfo *c3_r0;
  c3_nameCaptureInfo = NULL;
  c3_b_info = (c3_ResolvedFunctionInfo (*)[2])c3_info;
  (*c3_b_info)[0].context = "";
  (*c3_b_info)[0].name = "plus";
  (*c3_b_info)[0].dominantType = "double";
  (*c3_b_info)[0].resolved = "[B]plus";
  (*c3_b_info)[0].fileLength = 0U;
  (*c3_b_info)[0].fileTime1 = 0U;
  (*c3_b_info)[0].fileTime2 = 0U;
  (*c3_b_info)[1].context = "";
  (*c3_b_info)[1].name = "gt";
  (*c3_b_info)[1].dominantType = "double";
  (*c3_b_info)[1].resolved = "[B]gt";
  (*c3_b_info)[1].fileLength = 0U;
  (*c3_b_info)[1].fileTime1 = 0U;
  (*c3_b_info)[1].fileTime2 = 0U;
  sf_mex_assign(&c3_m0, sf_mex_createstruct("nameCaptureInfo", 1, 2));
  for (c3_i0 = 0; c3_i0 < 2; c3_i0 = c3_i0 + 1) {
    c3_r0 = &c3_info[c3_i0];
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", c3_r0->context, 15,
      0U, 0U, 0U, 2, 1, strlen(c3_r0->context)), "context",
                    "nameCaptureInfo", c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", c3_r0->name, 15, 0U,
      0U, 0U, 2, 1, strlen(c3_r0->name)), "name",
                    "nameCaptureInfo", c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", c3_r0->dominantType,
      15, 0U, 0U, 0U, 2, 1, strlen(c3_r0->dominantType)),
                    "dominantType", "nameCaptureInfo", c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", c3_r0->resolved, 15,
      0U, 0U, 0U, 2, 1, strlen(c3_r0->resolved)), "resolved"
                    , "nameCaptureInfo", c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", &c3_r0->fileLength,
      7, 0U, 0U, 0U, 0), "fileLength", "nameCaptureInfo",
                    c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", &c3_r0->fileTime1, 7,
      0U, 0U, 0U, 0), "fileTime1", "nameCaptureInfo", c3_i0);
    sf_mex_addfield(c3_m0, sf_mex_create("nameCaptureInfo", &c3_r0->fileTime2, 7,
      0U, 0U, 0U, 0), "fileTime2", "nameCaptureInfo", c3_i0);
  }

  sf_mex_assign(&c3_nameCaptureInfo, c3_m0);
  return c3_nameCaptureInfo;
}

static void init_dsm_address_info(void)
{
}

/* SFunction Glue Code */
void sf_c3_tanks_get_check_sum(mxArray *plhs[])
{
  ((real_T *)mxGetPr((plhs[0])))[0] = (real_T)(526622177U);
  ((real_T *)mxGetPr((plhs[0])))[1] = (real_T)(1982685967U);
  ((real_T *)mxGetPr((plhs[0])))[2] = (real_T)(2931563723U);
  ((real_T *)mxGetPr((plhs[0])))[3] = (real_T)(2402036830U);
}

mxArray *sf_c3_tanks_get_autoinheritance_info(void)
{
  const char *autoinheritanceFields[] = { "checksum", "inputs", "parameters",
    "outputs" };

  mxArray *mxAutoinheritanceInfo = mxCreateStructMatrix(1,1,4,
    autoinheritanceFields);

  {
    mxArray *mxChecksum = mxCreateDoubleMatrix(4,1,mxREAL);
    double *pr = mxGetPr(mxChecksum);
    pr[0] = (double)(740514881U);
    pr[1] = (double)(4127247680U);
    pr[2] = (double)(1070821189U);
    pr[3] = (double)(933981100U);
    mxSetField(mxAutoinheritanceInfo,0,"checksum",mxChecksum);
  }

  {
    const char *dataFields[] = { "size", "type", "complexity" };

    mxArray *mxData = mxCreateStructMatrix(1,1,3,dataFields);

    {
      mxArray *mxSize = mxCreateDoubleMatrix(1,2,mxREAL);
      double *pr = mxGetPr(mxSize);
      pr[0] = (double)(1);
      pr[1] = (double)(1);
      mxSetField(mxData,0,"size",mxSize);
    }

    {
      const char *typeFields[] = { "base", "fixpt" };

      mxArray *mxType = mxCreateStructMatrix(1,1,2,typeFields);
      mxSetField(mxType,0,"base",mxCreateDoubleScalar(10));
      mxSetField(mxType,0,"fixpt",mxCreateDoubleMatrix(0,0,mxREAL));
      mxSetField(mxData,0,"type",mxType);
    }

    mxSetField(mxData,0,"complexity",mxCreateDoubleScalar(0));
    mxSetField(mxAutoinheritanceInfo,0,"inputs",mxData);
  }

  {
    mxSetField(mxAutoinheritanceInfo,0,"parameters",mxCreateDoubleMatrix(0,0,
                mxREAL));
  }

  {
    const char *dataFields[] = { "size", "type", "complexity" };

    mxArray *mxData = mxCreateStructMatrix(1,1,3,dataFields);

    {
      mxArray *mxSize = mxCreateDoubleMatrix(1,2,mxREAL);
      double *pr = mxGetPr(mxSize);
      pr[0] = (double)(1);
      pr[1] = (double)(1);
      mxSetField(mxData,0,"size",mxSize);
    }

    {
      const char *typeFields[] = { "base", "fixpt" };

      mxArray *mxType = mxCreateStructMatrix(1,1,2,typeFields);
      mxSetField(mxType,0,"base",mxCreateDoubleScalar(10));
      mxSetField(mxType,0,"fixpt",mxCreateDoubleMatrix(0,0,mxREAL));
      mxSetField(mxData,0,"type",mxType);
    }

    mxSetField(mxData,0,"complexity",mxCreateDoubleScalar(0));
    mxSetField(mxAutoinheritanceInfo,0,"outputs",mxData);
  }

  return(mxAutoinheritanceInfo);
}

static void chart_debug_initialization(SimStruct *S, unsigned int
  fullDebuggerInitialization)
{
  if (ssIsFirstInitCond(S) && fullDebuggerInitialization==1) {
    /* do this only if simulation is starting */
    if (!sim_mode_is_rtw_gen(S)) {
      {
        unsigned int chartAlreadyPresent;
        chartAlreadyPresent = sf_debug_initialize_chart(_tanksMachineNumber_,
          3,
          1,
          1,
          2,
          0,
          0,
          0,
          0,
          0,
          &(chartInstance.chartNumber),
          &(chartInstance.instanceNumber),
          ssGetPath(S),
          (void *)S);
        if (chartAlreadyPresent==0) {
          /* this is the first instance */
          init_script_number_translation(_tanksMachineNumber_,
            chartInstance.chartNumber);
          sf_debug_set_chart_disable_implicit_casting(_tanksMachineNumber_,
            chartInstance.chartNumber,1);
          sf_debug_set_chart_event_thresholds(_tanksMachineNumber_,
            chartInstance.chartNumber,
            0,
            0,
            0);
          _SFD_SET_DATA_PROPS(0,2,0,1,SF_DOUBLE,0,NULL,0,0,0,0.0,1.0,0,"y",0,
                              NULL);
          _SFD_SET_DATA_PROPS(1,1,1,0,SF_DOUBLE,0,NULL,0,0,0,0.0,1.0,0,"tempo",0,
                              c3_sf_marshall);
          _SFD_STATE_INFO(0,0,2);
          _SFD_CH_SUBSTATE_COUNT(0);
          _SFD_CH_SUBSTATE_DECOMP(0);
        }

        _SFD_CV_INIT_CHART(0,0,0,0);

        {
          _SFD_CV_INIT_STATE(0,0,0,0,0,0,NULL,NULL);
        }

        _SFD_CV_INIT_TRANS(0,0,NULL,NULL,0,NULL);

        /* Initialization of EML Model Coverage */
        _SFD_CV_INIT_EML(0,1,3,0,0,0,0,0);
        _SFD_CV_INIT_EML_FCN(0,0,"eML_blk_kernel",0,-1,389);
        _SFD_CV_INIT_EML_IF(0,0,187,208,-1,232);
        _SFD_CV_INIT_EML_IF(0,1,234,251,-1,267);
        _SFD_CV_INIT_EML_IF(0,2,269,294,367,387);
        _SFD_TRANS_COV_WTS(0,0,0,1,0);
        if (chartAlreadyPresent==0) {
          _SFD_TRANS_COV_MAPS(0,
                              0,NULL,NULL,
                              0,NULL,NULL,
                              1,NULL,NULL,
                              0,NULL,NULL);
        }

        {
          real_T *c3_y;
          real_T *c3_tempo;
          c3_y = (real_T *)ssGetOutputPortSignal(chartInstance.S, 1);
          c3_tempo = (real_T *)ssGetInputPortSignal(chartInstance.S, 0);
          _SFD_SET_DATA_VALUE_PTR(0U, c3_y);
          _SFD_SET_DATA_VALUE_PTR(1U, c3_tempo);
        }
      }
    }
  } else {
    sf_debug_reset_current_state_configuration(_tanksMachineNumber_,
      chartInstance.chartNumber,chartInstance.instanceNumber);
  }
}

static void sf_opaque_initialize_c3_tanks(void *chartInstanceVar)
{
  chart_debug_initialization(chartInstance.S,0);
  initialize_params_c3_tanks();
  initialize_c3_tanks();
}

static void sf_opaque_enable_c3_tanks(void *chartInstanceVar)
{
  enable_c3_tanks();
}

static void sf_opaque_disable_c3_tanks(void *chartInstanceVar)
{
  disable_c3_tanks();
}

static void sf_opaque_gateway_c3_tanks(void *chartInstanceVar)
{
  sf_c3_tanks();
}

static mxArray* sf_opaque_get_sim_state_c3_tanks(void *chartInstanceVar)
{
  mxArray *st = (mxArray *) get_sim_state_c3_tanks();
  return st;
}

static void sf_opaque_set_sim_state_c3_tanks(void *chartInstanceVar, const
  mxArray *st)
{
  set_sim_state_c3_tanks(sf_mex_dup(st));
}

static void sf_opaque_terminate_c3_tanks(void *chartInstanceVar)
{
  if (sim_mode_is_rtw_gen(chartInstance.S) || sim_mode_is_external
      (chartInstance.S)) {
    sf_clear_rtw_identifier(chartInstance.S);
  }

  finalize_c3_tanks();
}

extern unsigned int sf_machine_global_initializer_called(void);
static void mdlProcessParameters_c3_tanks(SimStruct *S)
{
  int i;
  for (i=0;i<ssGetNumRunTimeParams(S);i++) {
    if (ssGetSFcnParamTunable(S,i)) {
      ssUpdateDlgParamAsRunTimeParam(S,i);
    }
  }

  if (sf_machine_global_initializer_called()) {
    initialize_params_c3_tanks();
  }
}

static void mdlSetWorkWidths_c3_tanks(SimStruct *S)
{
  if (sim_mode_is_rtw_gen(S) || sim_mode_is_external(S)) {
    int_T chartIsInlinable =
      (int_T)sf_is_chart_inlinable("tanks","tanks",3);
    ssSetStateflowIsInlinable(S,chartIsInlinable);
    ssSetRTWCG(S,sf_rtw_info_uint_prop("tanks","tanks",3,"RTWCG"));
    ssSetEnableFcnIsTrivial(S,1);
    ssSetDisableFcnIsTrivial(S,1);
    ssSetNotMultipleInlinable(S,sf_rtw_info_uint_prop("tanks","tanks",3,
      "gatewayCannotBeInlinedMultipleTimes"));
    if (chartIsInlinable) {
      ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
      sf_mark_chart_expressionable_inputs(S,"tanks","tanks",3,1);
      sf_mark_chart_reusable_outputs(S,"tanks","tanks",3,1);
    }

    sf_set_rtw_dwork_info(S,"tanks","tanks",3);
    ssSetHasSubFunctions(S,!(chartIsInlinable));
    ssSetOptions(S,ssGetOptions(S)|SS_OPTION_WORKS_WITH_CODE_REUSE);
  }

  ssSetChecksum0(S,(2236969599U));
  ssSetChecksum1(S,(1730726008U));
  ssSetChecksum2(S,(1448272081U));
  ssSetChecksum3(S,(509019144U));
  ssSetmdlDerivatives(S, NULL);
  ssSetExplicitFCSSCtrl(S,1);
}

static void mdlRTW_c3_tanks(SimStruct *S)
{
  if (sim_mode_is_rtw_gen(S)) {
    sf_write_symbol_mapping(S, "tanks", "tanks",3);
    ssWriteRTWStrParam(S, "StateflowChartType", "Embedded MATLAB");
  }
}

static void mdlStart_c3_tanks(SimStruct *S)
{
  chartInstance.chartInfo.chartInstance = NULL;
  chartInstance.chartInfo.isEMLChart = 1;
  chartInstance.chartInfo.chartInitialized = 0;
  chartInstance.chartInfo.sFunctionGateway = sf_opaque_gateway_c3_tanks;
  chartInstance.chartInfo.initializeChart = sf_opaque_initialize_c3_tanks;
  chartInstance.chartInfo.terminateChart = sf_opaque_terminate_c3_tanks;
  chartInstance.chartInfo.enableChart = sf_opaque_enable_c3_tanks;
  chartInstance.chartInfo.disableChart = sf_opaque_disable_c3_tanks;
  chartInstance.chartInfo.getSimState = sf_opaque_get_sim_state_c3_tanks;
  chartInstance.chartInfo.setSimState = sf_opaque_set_sim_state_c3_tanks;
  chartInstance.chartInfo.zeroCrossings = NULL;
  chartInstance.chartInfo.outputs = NULL;
  chartInstance.chartInfo.derivatives = NULL;
  chartInstance.chartInfo.mdlRTW = mdlRTW_c3_tanks;
  chartInstance.chartInfo.mdlStart = mdlStart_c3_tanks;
  chartInstance.chartInfo.mdlSetWorkWidths = mdlSetWorkWidths_c3_tanks;
  chartInstance.chartInfo.extModeExec = NULL;
  chartInstance.chartInfo.restoreLastMajorStepConfiguration = NULL;
  chartInstance.chartInfo.restoreBeforeLastMajorStepConfiguration = NULL;
  chartInstance.chartInfo.storeCurrentConfiguration = NULL;
  chartInstance.S = S;
  ssSetUserData(S,(void *)(&(chartInstance.chartInfo)));/* register the chart instance with simstruct */
  if (!sim_mode_is_rtw_gen(S)) {
    init_dsm_address_info();
  }

  chart_debug_initialization(S,1);
}

void c3_tanks_method_dispatcher(SimStruct *S, int_T method, void *data)
{
  switch (method) {
   case SS_CALL_MDL_START:
    mdlStart_c3_tanks(S);
    break;

   case SS_CALL_MDL_SET_WORK_WIDTHS:
    mdlSetWorkWidths_c3_tanks(S);
    break;

   case SS_CALL_MDL_PROCESS_PARAMETERS:
    mdlProcessParameters_c3_tanks(S);
    break;

   default:
    /* Unhandled method */
    sf_mex_error_message("Stateflow Internal Error:\n"
                         "Error calling c3_tanks_method_dispatcher.\n"
                         "Can't handle method %d.\n", method);
    break;
  }
}
